package siwi.map.android;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.wmw.Animation.ViewAction;
import com.wmw.DataPool.CommonData;
import com.wmw.Utility.Common;
import com.wmw.Utility.RegularExpression;

public class About extends SherlockActivity {
	// 版本更新 
	// 代码混淆减少apk的大小,中文字符串反编译乱码显示.
	public static final int _FEATURES_INTRODUCTION = 1;
	private TextView _version, _versionTips;

	private final static int _ImmediateRing = 0;
	private final static int _DelayRing = 1;

	TextView _immediateRingName, _delayRingName;
	ImageView _playImmediateRingName;
	int _curPlayingIdx;
	boolean _bPlaying;
	String[] musicNames;

	InputBox _realName;

	String DATA = MediaStore.Audio.Media.DATA;
	String TITLE = MediaStore.Audio.Media.TITLE;
	String DURATION = MediaStore.Audio.Media.DURATION;
	String ARTIST_ID = MediaStore.Audio.Media.ARTIST_ID;
	String ALBUM_ID = MediaStore.Audio.Media.ALBUM_ID;
	String YEAR = MediaStore.Audio.Media.YEAR;

	String ARTIST = MediaStore.Audio.Media.ARTIST;
	String ALBUM = MediaStore.Audio.Media.ALBUM;

	Uri uri_meta = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
	Intent _playMusicIntent;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initActionBar();

		setContentView(R.layout.about);

		try {
			initViews();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		initData();
	}
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == _FEATURES_INTRODUCTION) {
			Log.d(Common.GetLineInfo(), "-");
		}
	}
	private void initActionBar() {
		// Custom action bar.
		ActionBar actionBar = getSupportActionBar();
		ActionBar.LayoutParams params = new ActionBar.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
				Gravity.CENTER);
		View view = LayoutInflater.from(this).inflate(
				R.layout.action_bar_back_save, null);
		actionBar.setCustomView(view, params);
		actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		actionBar.setDisplayShowCustomEnabled(true);

		view.findViewById(R.id.ABBS_Save).setVisibility(View.GONE);
		
		findViewById(R.id.ABBS_Back).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ViewAction.startSpringMenuAnimations(v);
				// Go back.
				Log.d(Common.GetLineInfo(), "Go back without saving");
				goBack();
				return;
			}
		});
		
		TextView tv = (TextView) view.findViewById(R.id.ABBS_Title);
		tv.setText("关于");
	}

	private void initViews() throws Exception {
		_version = (TextView) findViewById(R.id.A_Version);
		_version.setText("V " + getVersionName());

		_versionTips = (TextView) findViewById(R.id.A_VersionTips);
		
		 findViewById(R.id.A_VersionUpdate).setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				if (!checkVersion())
					_versionTips.setText("已是最新版本");
				else{
					_versionTips.setText("存在新版本");
					updateVersion();
				}
			}
		 });
		 findViewById(R.id.A_FeaturesIntroduction).setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View arg0) {
					Intent intent = new Intent(About.this, FirstTimeGuide.class);
					startActivityForResult(intent, _FEATURES_INTRODUCTION);
				}
			 });
		 
	}

	private String getVersionName() throws Exception {
		// 获取packagemanager的实例
		PackageManager packageManager = getPackageManager();
		// getPackageName()是你当前类的包名，0代表是获取版本信息
		PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(),
				0);
		String version = packInfo.versionName;
		return version;
	}

	private void initData() {
		TextView tv = (TextView) findViewById(R.id.A_VersionTips);
		if (!checkVersion()){
			_versionTips.setText("已是最新版本");
		}
		else
			_versionTips.setText("存在新版本");
	}

	public void updateVersion(){
		if (checkVersion()) {
			// 发现新版本，提示用户更新

			// 开启更新服务UpdateService
			// 这里为了把update更好模块化，可以传一些updateService依赖的值
			// 如布局ID，资源ID，动态获取的标题,这里以app_name为例
//			Intent updateIntent = new Intent(
//					About.this, UpdateService.class);
//			updateIntent.putExtra(
//					"app_name",
//					getResources().getString(
//							R.string.app_name));
//			startService(updateIntent);
			// TODO
			Intent intent = new Intent();        
			intent.setAction("android.intent.action.VIEW");  
			Uri content_url = Uri.parse("http://www.baidupcs.com/file/c7856a2f1e675eefcfc6c5499acabd45?xcode=dc38b89be2541421abd016ec8eaa366d770f39f61880f1e1&fid=1812856341-250528-1616905580&time=1374585786&sign=FDTAXER-DCb740ccc5511e5e8fedcff06b081203-S%2BinGC3wXNqlwSAr3IWa45sIbfY%3D&to=wb&fm=N,N,T&expires=8h&rt=sh&r=625023844&logid=1560331946&sh=1");   
			intent.setData(content_url);
			startActivity(intent);
		}
	}
	/***
	 * 检查是否更新版本
	 */
	public boolean checkVersion() {
		ExApplication myApplication = (ExApplication) getApplication();
		return myApplication._LocalVersion < myApplication._ServerVersion;
	}
	
	private void goBack() {
		Intent intent = new Intent();
		intent.setClass(About.this, Home.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
		startActivity(intent);
		overridePendingTransition(R.anim.left_in, R.anim.right_out);
		About.this.finish();
	}
}
